﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AdminModel;
using System.Web.Script.Serialization;


namespace WinePro.Controllers
{
    public class HomeController : Controller
    {
        //WCF底层代码
        WinePro.Logic.logic logic = new Logic.logic();
        //
        // GET: /Home/
        /// <summary>
        /// 首页——酒帝网
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View(logic.GetGoodsMessage().Where(p => p.GoodsStart == 1 ).ToList());
        }
        /// <summary>
        /// 葡萄酒馆
        /// </summary>
        /// <returns></returns>
        public ActionResult Grape()
        {
            return View(logic.GetGoodsMessage().Where(p => p.GoodsStart == 1).ToList());
        }
        /// <summary>
        /// 洋酒馆
        /// </summary>
        /// <returns></returns>
        public ActionResult Imported()
        {
            return View(logic.GetGoodsMessage().Where(p => p.GoodsStart == 1).ToList());
        }
        /// <summary>
        /// 酒帝独家
        /// </summary>
        /// <returns></returns>
        public ActionResult Exclusive()
        {
            ViewBag.goodtypee = logic.GetGoodsType();
            return View(logic.GetGoodsMessage().Where(p => p.GoodsStart == 1).ToList());
        }
        /// <summary>
        /// 登陆——酒帝网
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            ViewBag.users = logic.GetUsers();
            ViewBag.usersrole = logic.GetUsersRole();
            return View();
        }
        public JsonResult LoginDo(string UsersName, string UsersPwd)
        {
            Users list = logic.GetUsers().Where(p => p.UsersName == UsersName && p.UsersPwd == Convert.ToInt32(UsersPwd)).FirstOrDefault();
            return Json(list);
        }
        /// <summary>
        /// 注册——酒帝网
        /// </summary>
        /// <returns></returns>
        public ActionResult Register()
        {
            return View();
        }
        public ActionResult RegisterAdd(Users u)
        {
            try
            {
                u.UsersName = u.UsersTel.ToString();
                u.UsersRoleID = 1;
                if (logic.AddUsers1(u) > 0)
                {
                    List<Users> list = logic.GetUsers().Where(p => p.UsersName == u.UsersName && p.UsersPwd == Convert.ToInt32(u.UsersPwd)).ToList();
                    return Content("<script>document.cookie='UsersName'+'='+escape(" + list[0].UsersName + ");document.cookie='UsersID'+'='+escape(" + list[0].UsersID + ");alert('添加成功！');location.href='/Home/Index'</script>");
                }
                else
                {
                    return Content("<script>alert('添加失败！');location.href='/Home/Index'</script>");
                }
            }
            catch
            {
                return Content("<script>alert('添加异常！');location.href='/Home/Index'</script>");
            }
        }

        /// <summary>
        /// 商品搜索——酒帝网
        /// </summary>
        /// <returns></returns>
        public ActionResult Search(string Sear)
        {
            List<GoodsMessage> list = new List<GoodsMessage>();
            foreach (var i in logic.GetGoodsMessage().Where(p => p.GoodsStart == 1 ).ToList())
            {
                string img = i.GoodsImg;
                JavaScriptSerializer jss = new JavaScriptSerializer();
                if (jss.Serialize(i).Contains(Sear))
                {
                    i.GoodsImg = img;
                    list.Add(i);
                }
            }
            return View(list);
        }
        /// <summary>
        /// 公告显示——酒帝网
        /// </summary>
        /// <returns></returns>
        public ActionResult Notice(int id)
        {
            return View(logic.GetNotice().Where(p => p.NoticeID == id).FirstOrDefault());
        }
        /// <summary>
        /// 详情页——商品详情
        /// </summary>
        /// <returns></returns>
        public ActionResult Details(int id)
        {
            ViewBag.goodtypee = logic.GetGoodsType();
            return View(logic.GetGoodsMessage().Where(p => p.GoodsID == id).FirstOrDefault());
        }

        public ActionResult ShopAdd(int GID, int UsersID)
        {
            AdminModel.Shop shop = new Shop();
            shop.GoodsID = GID;
            shop.IndentID = UsersID;
            shop.UsersID = UsersID; //用户编号
            shop.ShopDate = DateTime.Now;
            if (logic.AddShop(shop) > 0)
            {
                return Content("<script>alert('成功加入购物车');location.href='/Home/Shop/" + UsersID + "'</script>");
            }
            else
            {
                return Content("<script>alert('当前网络较慢,请稍后重试！');location.href='/Home/Details/" + GID + "'</script>");
            }
            
        }
        /// <summary>
        /// 购物车——酒帝网
        /// </summary>
        /// <returns></returns>
        public ActionResult Shop(int id)
        {
            ViewBag.users = logic.GetUsers();
            ViewBag.indent = logic.GetIndent();
            ViewBag.usersrelo = logic.GetUsersRole();
            ViewBag.area = logic.GetArea();
            ViewBag.goods = logic.GetGoodsMessage();
            ViewBag.goodtypee = logic.GetGoodsType();
            return View(logic.GetShop().Where(p => p.IndentID == id).ToList());
        }

        //public ActionResult ShopDele(int ShopID)
        //{
        //    AdminModel.Shop shop = new Shop();
        //    if (logic.DeleteShop(shop) > 0)
        //    {
        //        return Content("<script>alert('删除成功');location.href='/Home/Shop/" + ShopID + "'</script>");
        //    }
        //    else
        //    {
        //        return Content("<script>alert('当前网络较慢,请稍后重试！');location.href='/Home/Shop/" + ShopID + "'</script>");
        //    }
        //}
        public JsonResult AddClearing(int ID)
        {
            int InID = 0;
            AdminModel.Indent l = new Indent();
            l.UsersID = ID; ///地区获取数据
            l.AreaID = 20;
            l.ConsigneeAddr = "";
            l.ConsigneeName = "";
            l.ConsigneeTel = 1345566;
            l.IndentDate = DateTime.Now;
            l.IndentNumbers = "13445656";
            l.IndentPay = 0;
            l.Indentship = 0;
            l.IndentStart = 1;
            if (logic.AddIndent(l) > 0)
            {
                InID = logic.GetIndent().LastOrDefault().IndentID;
                foreach (var item in logic.GetShop().Where(p => p.IndentID == ID).ToList())
                {
                    AdminModel.Ind s = new Ind();
                    s.GoodsID = item.GoodsID;
                    s.IndentID = InID;
                    s.IndNum = 10; //购买数量
                    s.IndPrice = logic.GetGoodsMessage().Where(p => p.GoodsID == item.GoodsID).FirstOrDefault().GoodsPrice;
                    logic.AddInt(s);
                    logic.DeleteShop(item.ShopID);
                }
            }
            return Json(InID);

        }
        /// <summary>
        /// 订单结算——酒帝网
        /// </summary>
        /// <returns></returns>
        public ActionResult Clearing(int id)
        {
            ViewBag.users = logic.GetUsers();
            ViewBag.indent = logic.GetIndent();
            ViewBag.usersrelo = logic.GetUsersRole();
            ViewBag.area = logic.GetArea();
            ViewBag.goods = logic.GetGoodsMessage();
            ViewBag.goodtypee = logic.GetGoodsType();
            return View(logic.GetInt().Where(p => p.IndentID == id).ToList());
        }
        /// <summary>
        /// 订单支付——酒帝网
        /// </summary>
        /// <returns></returns>
        public ActionResult Pay(double Price, int id)
        {
            ViewBag.users = logic.GetUsers();
            ViewBag.indent = logic.GetIndent();
            ViewBag.usersrelo = logic.GetUsersRole();
            ViewBag.area = logic.GetArea();
            ViewBag.goods = logic.GetGoodsMessage();
            ViewBag.goodtypee = logic.GetGoodsType();
            ViewBag.price = Price;
            ViewBag.id = id;
            return View();
        }
        /// <summary>
        /// 支付宝支付页面——酒帝网
        /// </summary>
        /// <returns></returns>
        public ActionResult Payment(double Price, int id)
        {
            ViewBag.users = logic.GetUsers();
            ViewBag.indent = logic.GetIndent();
            ViewBag.usersrelo = logic.GetUsersRole();
            ViewBag.area = logic.GetArea();
            ViewBag.goods = logic.GetGoodsMessage();
            ViewBag.goodtypee = logic.GetGoodsType();
            ViewBag.price = Price;
            ViewBag.id = id;
            return View();
        }
        /// <summary>
        /// 用户中心——酒帝网
        /// </summary>
        /// <returns></returns>
        public ActionResult Users()
        {
            return View();
        }
        /// <summary>
        /// 基本信息——酒帝网
        /// </summary>
        /// <returns></returns>
        public ActionResult Basic()
        {
            ViewBag.usersrelo = logic.GetUsersRole();
            ViewBag.usersrelo = logic.GetUsersRole();
            ViewBag.indent = logic.GetIndent();
            return View(logic.GetInt().ToList());
        }
        /// <summary>
        /// 我的订单——酒帝网
        /// </summary>
        /// <returns></returns>
        public ActionResult Indent()
        {
            Session["date"] = DateTime.Now.ToString();
            ViewBag.users = logic.GetUsers();
            ViewBag.indent = logic.GetIndent();
            ViewBag.usersrelo = logic.GetUsersRole();
            ViewBag.area = logic.GetArea();
            ViewBag.goods = logic.GetGoodsMessage();
            ViewBag.goodtypee = logic.GetGoodsType();
            return View(logic.GetInt().Where(p => p.IndentID == 18).ToList());
        }
        /// <summary>
        /// 收货地址——酒帝网
        /// </summary>
        /// <returns></returns>
        public ActionResult Consignee()
        {
            return View();
        }
        /// <summary>
        /// 到货通知——酒帝网
        /// </summary>
        /// <returns></returns>
        public ActionResult AOG()
        {
            return View();
        }
        /// <summary>
        /// 我的收藏——酒帝网
        /// </summary>
        /// <returns></returns>
        public ActionResult Collect()
        {
            ViewBag.goods = logic.GetGoodsMessage();
            ViewBag.goodtypee = logic.GetGoodsType();
            return View(logic.GetCikkect().ToList());
        }
        /// <summary>
        /// 退货记录——酒帝网
        /// </summary>
        /// <returns></returns>
        public ActionResult Sales()
        {
            ViewBag.users = logic.GetUsers();
            ViewBag.indent = logic.GetIndent();
            return View(logic.GetRenturned().ToList());
        }
        /// <summary>
        /// 公告信息显示
        /// </summary>
        /// <returns></returns>
        public ActionResult GetNotice()
        {
            return Json(logic.GetNotice().Where(p => p.NoticeStart == 1).ToList());
        }

    }
}
